package com.tourlaid.mamagerment.util

import org.slf4j.LoggerFactory
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.support.PropertiesLoaderUtils
import java.util.*

/**
 * 加载配置文件
 * @author huanghaijun
 * *
 * @since 2015-07-08
 */
object ConfigUtil {
    private val log = LoggerFactory.getLogger(ConfigUtil::class.java)

    private var properties: Properties? = null

    init {
        try {
            val resource = ClassPathResource("/config.properties")
            properties = PropertiesLoaderUtils.loadProperties(resource)
        } catch (e: Exception) {
            log.error("读取配置文件出错", e)
        }

    }

    operator fun get(key: String?): String? {
        if (properties == null || key == null) {
            return null
        }
        return properties!!.getProperty(key)
    }

    /**
     * 指定默认值
     * @param key
     * *
     * @param defaultValue
     * *
     * @return
     */
    operator fun get(key: String?, defaultValue: String): String {
        if (properties == null || key == null) {
            return defaultValue
        }
        val value = properties!!.getProperty(key)

        return value ?: defaultValue
    }

}
